package de.hs.rm.jmx;

import javax.management.AttributeChangeNotification;
import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean{

	private String name = "test";
	private double multipliedDouble = 0.;
	private boolean great = false;
	private int printedInteger = 0;
	
	private int cacheSize = DEFAULT_CACHE_SIZE;
    private static final int DEFAULT_CACHE_SIZE = 200;
    private long sequenceNumber = 1;
	
	@Override
	public void setName(String name) {
		this.name = name;
		
	}
	
	@Override
	public int getCacheSize() {
	        return this.cacheSize;
	}
	
	@Override
	public synchronized void setCacheSize(int size) {
        int oldSize = this.cacheSize;
        this.cacheSize = size;

        System.out.println("Cache size now " + this.cacheSize);

        Notification n = new AttributeChangeNotification(this,
                                sequenceNumber++, System.currentTimeMillis(),
                                "CacheSize changed", "CacheSize", "int",
                                oldSize, this.cacheSize);
        sendNotification(n);
    }
	
	@Override
    public MBeanNotificationInfo[] getNotificationInfo() {
        String[] types = new String[]{
            AttributeChangeNotification.ATTRIBUTE_CHANGE
        };

        String name = AttributeChangeNotification.class.getName();
        String description = "An attribute of this MBean has changed";
        MBeanNotificationInfo info = 
                new MBeanNotificationInfo(types, name, description);
        return new MBeanNotificationInfo[]{info};
    }

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void printName() {
		System.out.println("######################################"
				+ "\nName: " + this.name
				+ "\nMuliplied double: " + this.multipliedDouble 
				+ "\nPrinted integer: " + printedInteger
				+" \n######################################");
		
	}

	@Override
	public boolean multiplyDouble (double x, double y) {
		multipliedDouble = x * y;
		if (multipliedDouble > 20.)
			return false;
		else
			return true;
	}

	@Override
	public boolean isGreat() {
		// TODO Auto-generated method stub
		return great;
	}

	@Override
	public double getMultipliedDouble() {
		// TODO Auto-generated method stub
		return multipliedDouble;
	}

	@Override
	public void setPrintedInteger(int printedInteger) {
		this.printedInteger = printedInteger;
		
	}

}
